package com.applet.applet_shangcheng.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.applet.applet_shangcheng.dao.*;
import com.applet.applet_shangcheng.entity.IntegralGoods;
import com.applet.applet_shangcheng.entity.IntegralMalls;
import com.applet.applet_shangcheng.utlis.PageResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import jdk.nashorn.internal.parser.JSONParser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Array;
import java.util.*;

@Service
@Transactional
public class IntegralService {

    @Autowired
    private IntegralMapper integralMapper;

    @Autowired
    private IntegralGoodsMapper integralGoodsMapper;
    @Autowired
    private IntegralOrderMapper integralOrderMapper;
    @Autowired
    private ImagesMapper imagesMapper;

    public List<IntegralMalls> findAll() {
        List<IntegralMalls> mallsList = integralMapper.findAll();
        return mallsList;
    }

    ;

    public int getHasPay(String month) {
        int hasPay = integralOrderMapper.getHasPay(month);
        return hasPay;
    }

    public int getNotPay(String month) {
        int NotPay = integralOrderMapper.getNotPay(month);
        return NotPay;
    }

    public Map selectGoods(String sortId) {
        Map<String, Object> map = new HashMap<>();

        List<IntegralGoods> integral1 = integralGoodsMapper.selectGoods(sortId, 0, 20);
        List<IntegralGoods> integral2 = integralGoodsMapper.selectGoods(sortId, 20, 40);
        if (integral1 != null) {
            map.put("integral0_20", integral1);
        }
        if (integral2 != null) {
            map.put("integral20_40", integral2);
        }
        int integral = integralGoodsMapper.findByIntegral();
        List<IntegralGoods> integral3 = integralGoodsMapper.selectGoods(sortId, 40, 80);
        List<IntegralGoods> integral4 = integralGoodsMapper.selectGoods(sortId, 80, 100);
        List<IntegralGoods> integral5 = integralGoodsMapper.selectGoodsAndMax(sortId);
        if (integral3 != null) {
            map.put("integral40_80", integral3);
        }
        if (integral4 != null) {
            map.put("integral80_100", integral4);
        }
        if (integral5 != null) {
            map.put("integral_100Above", integral5);
        }
        return map;
    }

    ;

    public IntegralGoods findById(int id) {
        IntegralGoods integralGoods = integralGoodsMapper.findById(id);
        String images = integralGoods.getImages();
        if (images != null) {
            JSONArray objects = JSON.parseArray(images);
            List<String> list = new ArrayList<>();
            if (integralGoods.getImage()!=null){
                list.add(integralGoods.getImage());
            }
            for (Object object : objects) {
                list.add(object.toString());
            }
            integralGoods.setImagess(list);
        }
        return integralGoods;
    }

    public int insertIntegralGoods(IntegralGoods integralGoods) {
        IntegralGoods goods = integralGoodsMapper.getGoodsId(integralGoods.getGoodsId());
        if (goods != null) {
            if (goods.getGoodsId() == integralGoods.getGoodsId()) {
                return 0;
            }
        }
        IntegralGoods goods1 = integralGoodsMapper.getGoodsName(integralGoods.getGoodsName());
        if (goods1 != null) {
            if (goods1.getGoodsName().equals(integralGoods.getGoodsName())) {
                return 0;
            }
        }
        integralGoods.setCreateTime(new Date());
        int result = integralGoodsMapper.insertIntegralGoods(integralGoods);
        return result;
    }

    public int updateGoods(IntegralGoods integralGoods) {
        IntegralGoods goods = integralGoodsMapper.findById(integralGoods.getId());
        if (goods.getImage()!=null){
            imagesMapper.updateImageStatus(goods.getImage());
        }
        /*String images = goods.getImages();
        if (images != null&& images.equalsIgnoreCase("null")) {
            JSONArray objects = JSON.parseArray(images);
            for (Object object : objects) {
                imagesMapper.updateImageStatus(object.toString());
            }
        }*/
        integralGoods.setUpdateTime(new Date());
        int result = integralGoodsMapper.updateGoods(integralGoods);
        return result;
    }

    ;

    public int deleteGoods(int id) {
        int result = integralGoodsMapper.deleteGoods(id);
        return result;
    }

    ;

    public PageResult findAll1(String queryString, Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        Page<IntegralGoods> page = integralGoodsMapper.findAll1(queryString);
        //PageInfo<Announcement> pageInfo = new PageInfo<>(announcementList);
        return new PageResult(page.getTotal(), page.getResult());
    }

    public PageResult calssFindAll(String queryString, Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        Page<IntegralMalls> page = integralGoodsMapper.calssFindAll(queryString);
        //PageInfo<Announcement> pageInfo = new PageInfo<>(announcementList);
        return new PageResult(page.getTotal(), page.getResult());
    }

    public IntegralGoods findByIdAll(int id) {
        return integralGoodsMapper.findByIdAll(id);
    }

    public static void main(String[] args) {
        String images = "[\"http://localhost:8900/ImageFiles/image/9124eac389394a4998d932f84cb7059a.jpeg\",\"http://localhost:8900/ImageFiles/image/03b45082da144d49afc09fd4eb4b12aa.jpeg\",\"http://localhost:8900/ImageFiles/image/296f1bb77fdf43d0adef78ee4c27534b.jpeg\",\"http://localhost:8900/ImageFiles/image/8db8b2918da241038f6d233ae73d532e.jpeg\",\"http://localhost:8900/ImageFiles/image/044743eb953641978866bc3191454b8c.jpg\"]";
        if (images != null) {
            JSONArray objects = JSON.parseArray(images);
            for (Object object : objects) {
                System.out.println(object);
            }
        }
    }

    public int deleteGoodsclass(int id) {
        String idD = integralMapper.findByGoodsClassId(id);
        List<IntegralGoods> integralGoods = integralMapper.findByClassId(Integer.parseInt(idD));
        if (integralGoods.size() > 0) {
            return 0;
        }
        return integralGoodsMapper.deleteGoodsclass(id);
    }

    public int insertGoodsClass(IntegralMalls integralMalls) {
        integralMalls.setTableT("tb_integral_goods");
        return integralGoodsMapper.insertGoodsClass(integralMalls);
    }

    public IntegralMalls findBySortId(int parseInt) {
        return integralGoodsMapper.findBySortId(parseInt);
    }

    public List<IntegralGoods> findAllS() {
        return integralGoodsMapper.findAll();
    }
}
